package com.ruoyi.oa.leave.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.EntityToQueryWrapperConverter;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.leave.domain.LeaveInfo;
import com.ruoyi.oa.leave.service.ILeaveInfoService;

/**
 * 请假申请单Controller
 * 
 * @author qibotian
 * @date 2025-02-19
 */
@RestController
@RequestMapping("/oa/leave")
public class LeaveInfoController extends BaseController
{
    @Autowired
    private ILeaveInfoService leaveInfoService;

    /**
     * 查询请假申请单列表
     */
    @PreAuthorize("@ss.hasPermi('oa:leave:list')")
    @GetMapping("/list")
    public TableDataInfo list(LeaveInfo leaveInfo)
    {
        startPage();
        QueryWrapper<LeaveInfo> queryWrapper = new QueryWrapper<>(leaveInfo);
        List<LeaveInfo> list = leaveInfoService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出请假申请单列表
     */
    @PreAuthorize("@ss.hasPermi('oa:leave:export')")
    @Log(title = "请假申请单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LeaveInfo leaveInfo)
    {
    	QueryWrapper<LeaveInfo> queryWrapper = new QueryWrapper<>(leaveInfo);
        List<LeaveInfo> list = leaveInfoService.list(queryWrapper);
        ExcelUtil<LeaveInfo> util = new ExcelUtil<LeaveInfo>(LeaveInfo.class);
        util.exportExcel(response, list, "请假申请单数据");
    }

    /**
     * 获取请假申请单详细信息
     */
    @PreAuthorize("@ss.hasPermi('oa:leave:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(leaveInfoService.getById(id));
    }

    /**
     * 新增请假申请单
     */
    @PreAuthorize("@ss.hasPermi('oa:leave:add')")
    @Log(title = "请假申请单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LeaveInfo leaveInfo)
    {
        return toAjax(leaveInfoService.save(leaveInfo));
    }

    /**
     * 修改请假申请单
     */
    @PreAuthorize("@ss.hasPermi('oa:leave:edit')")
    @Log(title = "请假申请单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LeaveInfo leaveInfo)
    {
        return toAjax(leaveInfoService.saveOrUpdate(leaveInfo));
    }

    /**
     * 删除请假申请单
     */
    @PreAuthorize("@ss.hasPermi('oa:leave:remove')")
    @Log(title = "请假申请单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(leaveInfoService.removeByIds(Arrays.asList(ids)));
    }
}
